package com.hzit;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

/**
 * 类名：
 * 作者：WF
 * 功能：mybatis工具类
 */
public class MybatisUtil {

	//1. 得到sqlsessionFactory
	private static SqlSessionFactory sqlSessionFactory(){
		SqlSessionFactory sqlSessionFactory = null;
		try {
			String resource = "mybatis/mybatis-config.xml";
			InputStream inputStream = Resources.getResourceAsStream(resource);
			sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sqlSessionFactory;
	}
	//2. 根据sqlsessionFactory得到SqlSession (相当于数据库连接)
	public static SqlSession getSession(boolean isAutoCommit){
		SqlSessionFactory sqlSessionFactory = sqlSessionFactory();
		return sqlSessionFactory.openSession(isAutoCommit);
	}
}
